import {history} from "../App";
import services from "../services";

let idGenerator = 10;

let initialState = {
    products: services.product.query(),
    //todo 页面刷新,如何获取当前参数:id和对象
    product: {}
}

export default {
    namespace: "products",
    state: initialState,
    reducers: {
        add: (state) => {
            services.product.add({id: "" + idGenerator++, name: "商品" + idGenerator, price: idGenerator})
            return {...state, products: services.product.query()};
        },
        /**
         * 删除逻辑
         * @param state
         * @param id
         * @returns {*}
         */
        del: (state, {payload: id}) => {
            services.product.del(id);
            return {...state, products: services.product.query()};
        },
        edit: (state, {payload: product}) => {
            //保存产品
            services.product.edit(product);
            //页面跳转
            let stateNew = {...state, products: services.product.query()};
            history.push("/product");
            return stateNew;
        },
        detail: (state, {payload: id}) => {
            return {...state, product: services.product.detail(id)};
        }
    },
};